package com.timbo.service.system.impl;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.timbo.dao.system.SysDepartDao;
import com.timbo.dao.system.SysFunctionDao;
import com.timbo.dao.system.SysIconDao;
import com.timbo.dao.system.SysRoleDao;
import com.timbo.dao.system.SysRoleFunctionDao;
import com.timbo.dao.system.SysRoleUserDao;
import com.timbo.dao.system.SysUserOrgDao;
import com.timbo.dao.system.SysUserDao;
import com.timbo.model.system.SysDepart;
import com.timbo.model.system.SysFunction;
import com.timbo.model.system.SysIcon;
import com.timbo.model.system.SysRole;
import com.timbo.model.system.SysRoleFunction;
import com.timbo.model.system.SysRoleUser;
import com.timbo.model.system.SysUserOrg;
import com.timbo.model.system.SysUser;
import com.timbo.service.system.ISysUserSV;
import com.timbo.util.db.Criteria;

@Service("sysUserSV")
@Transactional
public class SysUserSVImpl implements ISysUserSV {

	@Autowired
	private SysUserDao<SysUser> sysUserDao;
	@Autowired
	private SysUserOrgDao<SysUserOrg> sysUserOrgDao;
	@Autowired
	private SysDepartDao<SysDepart> sysDepartDao;
	@Autowired
	private SysRoleUserDao<SysRoleUser> sysRoleUserDao;
	@Autowired
	private SysRoleDao<SysRole> sysRoleDao;
	@Autowired
	private SysRoleFunctionDao<SysRoleFunction> sysRoleFunctionDao;
	@Autowired
	private SysFunctionDao<SysFunction> sysFunctionDao;
	@Autowired
	private SysIconDao<SysIcon> sysIconDao;

	@Override
	public List<SysUser> query(SysUser sysUser) {
		
		List<SysUser> sysUsers = sysUserDao.query(sysUser);
		return sysUsers;
	}
	
	@Override
	public SysUser checkUserExits(String username, String password) {
		
		SysUser sysUser = new SysUser();
		Criteria criteria = new Criteria();
		criteria.add("username", username);
		criteria.add("password", password);
		sysUser.setCriteria(criteria);
		List<SysUser> users = sysUserDao.query(sysUser);
		
		return (users != null && users.size()>0)? users.get(0): null;
	}

	@Override
	public List<String> queryOrgIdsByUserId(String userId) {
		
		List<String> orgIds = sysUserOrgDao.queryOrgIdsByUserId(userId);
		return orgIds;
	}

	@Override
	public List<SysDepart> queryDepartsByUserId(String userId) {
		
		List<String> orgIds = sysUserOrgDao.queryOrgIdsByUserId(userId);
		List<SysDepart> departList = sysDepartDao.queryDepartsByIds(orgIds);
		return departList;
	}

	@Override
	public SysUser queryUserById(String id) {
		
		SysUser inSysUser = new SysUser();
		inSysUser.setId(id);
		List<SysUser> sysUsers = sysUserDao.query(inSysUser);
		return sysUsers!=null? sysUsers.get(0): null;
	}

	@Override
	public List<SysRole> queryRolesByUserId(String userId) {
		List<String> roleIds = sysRoleUserDao.queryRoleIdsByUserId(userId);
		List<SysRole> roles = sysRoleDao.queryRolesByIds(roleIds);
		return roles;
	}

	@Override
	public List<SysFunction> queryFunctionsByUserId(String userId) {
		
		List<SysFunction> sysFunctions = sysFunctionDao.queryFunctionsByUserId(userId);
		for (SysFunction sysFunction: sysFunctions) {
			if (StringUtils.isNotBlank(sysFunction.getIconid())) {
				SysIcon sysIcon = sysIconDao.queryById(sysFunction.getIconid());
				sysFunction.setSysIcon(sysIcon);
			}
			if (StringUtils.isNotBlank(sysFunction.getDeskIconid())) {
				SysIcon deskSysIcon = sysIconDao.queryById(sysFunction.getDeskIconid());
				sysFunction.setDeskSysIcon(deskSysIcon);
			}
			if (StringUtils.isNotBlank(sysFunction.getParentfunctionid())) {
				SysFunction parentSysFunction = sysFunctionDao.queryById(sysFunction.getParentfunctionid());
				sysFunction.setParentSysFunction(parentSysFunction);
			}
		}
		return sysFunctions;
	}

	@Override
	public int updateUser(SysUser sysUser) {
		
		return sysUserDao.update(sysUser);
	}

	@Override
	public List<SysUser> queryUsers(SysUser user) {
		
		String orgIds = user.getParam1();
		String status = user.getParam2();
		Criteria criteria = new Criteria();
		if (StringUtils.isNotBlank(orgIds)) {
			
		}
		if (StringUtils.isNotBlank(status)) {
			List<String> statusList = Arrays.asList(status.split(","));
			criteria.addIn("status", statusList);
		}
		List<SysUser> userList = sysUserDao.query(user);
		return userList;
	}

}
